#include "takecolor.h"
#include "pimage.h"

//************************************************************* Constructor

TakeColor::TakeColor(DrawingArea* drawingAreaUse,QDockWidget* dockInfoToolP): ToolEffect(drawingAreaUse){
    dockInfoTool = dockInfoToolP;
}

//************************************************************* Recup Color

void TakeColor::recupColor(QPointF coord, Layer* layer, PImage* pimage){
    QPoint coordonnee = coord.toPoint();
    coordonnee.setX(coordonnee.x() - layer->pos().x());
    coordonnee.setY(coordonnee.y() - layer->pos().y());

    // If we try to take the color out of the image the we mustn't show a new color
    if(coordonnee.x() < 0 || coordonnee.x() > layer->getImage()->width() ||
       coordonnee.y() < 0 || coordonnee.y() > layer->getImage()->height())
    {
        pimage->getUiColors()->modifLabel(0, 0, 0, 0, 0, 0, 0);
    }else{
        QRgb rgb = layer->getImage()->pixel(coordonnee);
        Yuv* yuv = new Yuv(rgb);

        int red = qRed(rgb);
        int green = qGreen(rgb);
        int blue = qBlue(rgb);

        int y = yuv->getY();
        int u = yuv->getU();
        int v = yuv->getV();

        pimage->getUiColors()->modifLabel(red, green, blue, y, u, v, qAlpha(rgb));
    }
}

//************************************************************* Affichage of Dock Tool Info

void TakeColor::createAffichage(){
    QWidget* widgetInfoToolUp = new QWidget();
    QWidget* widgetInfoTool = new QWidget(widgetInfoToolUp);
    QGridLayout* layoutInfoTool = new QGridLayout();

    QLabel *labelInfoGenTool = new QLabel();

    labelInfoGenTool->setText(QString::fromUtf8("Sélection d'une couleur : cliquez sur l'image pour récupérer la couleur"));

    layoutInfoTool->addWidget(labelInfoGenTool, 0, 0, Qt::AlignVCenter);

    widgetInfoTool->setLayout(layoutInfoTool);
    dockInfoTool->setWidget(widgetInfoToolUp);
}
